package com.vnet.bdz.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.RootPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.XTemplate;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.data.XmlReader;
import com.gwtext.client.data.Record.Operation;
import com.gwtext.client.data.event.StoreListener;
import com.gwtext.client.widgets.DataView;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.DataViewListenerAdapter;
import com.gwtext.client.widgets.layout.VerticalLayout;

public class Main implements EntryPoint {
	@Override
	public void onModuleLoad() {
    
		setupContentFrame();
		setupMenu();
		setupWeatherView();
		
        RootPanel.get("ladowanie").setVisible(false);
        RootPanel.get("lane2").add(artykuly);
        RootPanel.get("lane1").add(panelLinki);
        RootPanel.get("whether").add(wDataView);
	}

    final DataView wDataView = new DataView("div.pogoda");
	private void setupWeatherView() {
        XmlReader wreader = new XmlReader("current_conditions", new RecordDef(   
                new FieldDef[]{   
                        new StringFieldDef("condition", "condition/@data"),   
                        new StringFieldDef("icon", "icon/@data"),   
                        new StringFieldDef("icon", "icon/@data"),
                        new StringFieldDef("temp_c", "temp_c/@data"),
                        new StringFieldDef("wind_condition", "wind_condition/@data"),
                        new StringFieldDef("humidity", "humidity/@data")
                }   
        ));   
        
        SiteServiceAsync weatherService = GWT.create(SiteService.class);
        final Store wstore = new Store(wreader);   

        AsyncCallback<String> callback = new AsyncCallback<String>() 
        {
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onSuccess(String result) {
				wstore.loadXmlData(result, false);
			}  
        };

        weatherService.getWeatherXML(callback);
        
        final XTemplate wtemplate = new XTemplate(new String[]{   
        		"<tpl for='.'>",
        		"<div class='pogoda'>Pogoda na dzisiaj<br>",
        		"<img src='{icon}' ext:qtip='{condition}'><br>",   
                "<span ext:qtip='Zachmurzenie'><strong>{condition}</strong></span><br>",
                "<span ext:qtip='Temperatura i Wilgotność'>Temperatura: {temp_c}C {humidity}</span><br>",
                "<span ext:qtip='Wiatr'>{wind_condition}</span>",
                "</div>",
                "</tpl>"});  

        wDataView.setStore(wstore);
        wDataView.setTpl(wtemplate);

        wstore.addStoreListener(new StoreListener(){

			@Override
			public boolean doBeforeLoad(Store store) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void onAdd(Store store, Record[] records, int index) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onClear(Store store) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onDataChanged(Store store) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onLoad(Store store, Record[] records) {
				wDataView.refresh();
			}

			@Override
			public void onLoadException(Throwable error) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onRemove(Store store, Record record, int index) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onUpdate(Store store, Record record, Operation operation) {
				// TODO Auto-generated method stub
				
			}});
	}

	final Frame artykuly = new Frame("glowna.html");
	private void setupContentFrame() {
		artykuly.setSize("100%", "750px");
		DOM.setElementProperty(artykuly.getElement(), "frameBorder", "0"); 	
	}

	Panel panelLinki = new Panel();
	private void setupMenu()
	{
		panelLinki.setCollapsible(true);
		panelLinki.setTitle("Idź do...");
		panelLinki.setId("images-view");   
        panelLinki.setBorder(false);   
        panelLinki.setPaddings(0);
        panelLinki.setFrame(false);   
        panelLinki.setLayout(new VerticalLayout(2));
        
        MemoryProxy dataProxy = new MemoryProxy(getData());   
        RecordDef recordDef = new RecordDef(new FieldDef[]{   
        		new StringFieldDef("imgurl"),
        		new StringFieldDef("name"),   
                new StringFieldDef("url"),
                new StringFieldDef("size"),
        }); 

        ArrayReader reader = new ArrayReader(recordDef);
        
        XTemplate template = new XTemplate(new String[]{   
                "<tpl for='.'>", 
                "<div class='thumb-wrap'>",   
                "<img src='{imgurl}' ext:qtip='{name}'>",   
                "<span ext:qtip='{name}'>{name}</span></div>",   
                "</tpl>" 
        });   
        
        Store linki = new Store(dataProxy,reader);
        linki.load();
        
        DataView menuDataView = new DataView("div.thumb-wrap");
        menuDataView.setStore(linki);
        menuDataView.setTpl(template);
        menuDataView.setAutoHeight(true);   
        menuDataView.setMultiSelect(false);   
        menuDataView.setOverCls("x-view-over");   
        menuDataView.setEmptyText("No Images to display");  
        menuDataView.addListener(new DataViewListenerAdapter(){
        	
			public void onClick(DataView source, int index, Element node,
					EventObject e) {
				artykuly.setUrl((String) getData()[index][2]);
				artykuly.setHeight((String) getData()[index][3]);
			}
        });
        panelLinki.add(menuDataView);
	}
	
	private Object[][] getData() {   
        return new Object[][]{   
                new Object[]{"images/61.png","Strona główna","glowna.html", "750px"},   
                new Object[]{"images/38.png","Zarząd klubu","kontakt.html", "750px"},
                new Object[]{"images/37a.png","Zawodnicy","zawodnicy.html", "1500px"},
                new Object[]{"images/33.png","Wydarzenia","wydarzenia.html", "1500px"},
                new Object[]{"images/33.png","Galerie","galeria.html", "1500px"},
                new Object[]{"images/49.png","Puchar Burmistrza","puchar.html", "1500px"},
                new Object[]{"images/49.png","Harmonogram treningów","harmonogram.html", "502px"}       
        };   
	}
}
